<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Database\Mappers;

use NRG\Xnat as Xnat;
use NRG\Exceptions as Exceptions;
use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Framework\Exceptions\QueryException as QueryException;

/**
 * MR Assessor mapper
 *
 * @package NRG\Database\Mappers
 */
class MrAssessor extends Mapper
{
    /**
     * Assessor ID
     *
     * @var string
     */
    protected $_id;

    /**
     * Constructor
     */
    public function __construct()
    {
	parent::__construct();
    }

    /**
     * Retrieve MR Assessor by ID
     *
     * @param string $id MR Assessor ID
     * @return NRG\Xnat\MrAssessor
     */
    public function getAssessor($id=null)
    {
	// input validation
	if(!is_string($id))
	    throw new IllegalArgumentException("Assessor ID must be a string");

	if($id == "")
	    throw new IllegalArgumentException("Assessor ID cannot be null");

	$query = "SELECT xmad.id AS assessor_id, xiad.imagesession_id AS session_id FROM xnat_mrassessordata xmad, " .
		"xnat_imageassessordata xiad WHERE (xmad.id=xiad.id) AND (xmad.id=$1);";

	$params = array($id);
	$result = $this->_xnat->query($query, $params);

	if(count($result) == 0)
	    throw new QueryException(QueryException::EMPTY_RESULT, $query, $params);
	else if(count($result) > 1)
	    throw new QueryException(QueryException::MULTI_ROW, $query, $params);

	$result = $result[0];

	$assessor = new Xnat\MrAssessor();
	$assessor->setID($result["assessor_id"]);
	$assessor->setParentID($result["session_id"]);

	return $assessor;
    }
}
